package com.zmn.plat.admin.controller.part.relation;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zmn.plat.admin.controller.part.PartBaseController;
import com.zmn.plat.model.entity.part.PartSku;
import com.zmn.plat.model.entity.part.PartSkuQuery;
import com.zmn.plat.model.entity.part.PartSkuRelationDTO;

/**
 * 类描述： PartSkuRelationListController
 *
 * @author XiangJunMing
 * @since 2019/03/23 20:36
 */
@Controller
@RequestMapping("/part/relation")
public class PartSkuRelationListController extends PartBaseController {

    private Logger logger = LoggerFactory.getLogger(PartSkuRelationListController.class);

    /**
     * 描述: 跳转到配件关联页（添加修改）
     *
     * @author XiangJunMing
     * @since 2019/03/23 20:34
     */
    @RequestMapping(value = {"/addEdit"})
    @RequiresPermissions("/part/relation/list.action")
    public String addedit(@RequestParam(name = "skuId", required = false) Integer skuId, Model model) {
        List<PartSkuRelationDTO> relations = partSkuRelationService.listByOneSkuId(skuId);
        PartSkuQuery query = new PartSkuQuery();
        query.setStatus(new Integer(2).shortValue());
        List<PartSku> skus =
            partSkuService.listByQuery(query).stream().filter(item -> !Objects.equals(skuId, item.getSkuId())).collect(Collectors.toList());
        model.addAttribute("relations", relations);
        model.addAttribute("skus", skus);
        model.addAttribute("oneSkuId", skuId);
        return "part/relation/addedit";
    }

}
